<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>base64.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_crypt_base64.js.html">base64.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Base64 en/decoding. Not much to say here except that we
<a name="line17"></a> * work with decoded values in arrays of bytes. By &quot;byte&quot; I mean a number
<a name="line18"></a> * in [0, 255].
<a name="line19"></a> *
<a name="line20"></a> * @author doughtie@google.com (Gavin Doughtie)
<a name="line21"></a> * @author fschneider@google.com (Fritz Schneider)
<a name="line22"></a> */
<a name="line23"></a>
<a name="line24"></a>goog.provide(&#39;goog.crypt.base64&#39;);
<a name="line25"></a>goog.require(&#39;goog.crypt&#39;);
<a name="line26"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line27"></a>
<a name="line28"></a>// Static lookup maps, lazily populated by init_()
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * Maps bytes to characters.
<a name="line33"></a> * @type {Object}
<a name="line34"></a> * @private
<a name="line35"></a> */
<a name="line36"></a>goog.crypt.base64.byteToCharMap_ = null;
<a name="line37"></a>
<a name="line38"></a>
<a name="line39"></a>/**
<a name="line40"></a> * Maps characters to bytes.
<a name="line41"></a> * @type {Object}
<a name="line42"></a> * @private
<a name="line43"></a> */
<a name="line44"></a>goog.crypt.base64.charToByteMap_ = null;
<a name="line45"></a>
<a name="line46"></a>
<a name="line47"></a>/**
<a name="line48"></a> * Maps bytes to websafe characters.
<a name="line49"></a> * @type {Object}
<a name="line50"></a> * @private
<a name="line51"></a> */
<a name="line52"></a>goog.crypt.base64.byteToCharMapWebSafe_ = null;
<a name="line53"></a>
<a name="line54"></a>
<a name="line55"></a>/**
<a name="line56"></a> * Maps websafe characters to bytes.
<a name="line57"></a> * @type {Object}
<a name="line58"></a> * @private
<a name="line59"></a> */
<a name="line60"></a>goog.crypt.base64.charToByteMapWebSafe_ = null;
<a name="line61"></a>
<a name="line62"></a>
<a name="line63"></a>/**
<a name="line64"></a> * Our default alphabet, shared between
<a name="line65"></a> * ENCODED_VALS and ENCODED_VALS_WEBSAFE
<a name="line66"></a> * @type {string}
<a name="line67"></a> */
<a name="line68"></a>goog.crypt.base64.ENCODED_VALS_BASE =
<a name="line69"></a>    &#39;ABCDEFGHIJKLMNOPQRSTUVWXYZ&#39; +
<a name="line70"></a>    &#39;abcdefghijklmnopqrstuvwxyz&#39; +
<a name="line71"></a>    &#39;0123456789&#39;;
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>/**
<a name="line75"></a> * Our default alphabet. Value 64 (=) is special; it means &quot;nothing.&quot;
<a name="line76"></a> * @type {string}
<a name="line77"></a> */
<a name="line78"></a>goog.crypt.base64.ENCODED_VALS =
<a name="line79"></a>    goog.crypt.base64.ENCODED_VALS_BASE + &#39;+/=&#39;;
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * Our websafe alphabet.
<a name="line84"></a> * @type {string}
<a name="line85"></a> */
<a name="line86"></a>goog.crypt.base64.ENCODED_VALS_WEBSAFE =
<a name="line87"></a>    goog.crypt.base64.ENCODED_VALS_BASE + &#39;-_.&#39;;
<a name="line88"></a>
<a name="line89"></a>
<a name="line90"></a>/**
<a name="line91"></a> * Whether this browser supports the atob and btoa functions. This extension
<a name="line92"></a> * started at Mozilla but is now implemented by many browsers. We use the
<a name="line93"></a> * ASSUME_* variables to avoid pulling in the full useragent detection library
<a name="line94"></a> * but still allowing the standard per-browser compilations.
<a name="line95"></a> *
<a name="line96"></a> * @type {boolean}
<a name="line97"></a> */
<a name="line98"></a>goog.crypt.base64.HAS_NATIVE_SUPPORT = goog.userAgent.GECKO ||
<a name="line99"></a>                                       goog.userAgent.WEBKIT ||
<a name="line100"></a>                                       goog.userAgent.OPERA ||
<a name="line101"></a>                                       typeof(goog.global.atob) == &#39;function&#39;;
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * Base64-encode an array of bytes.
<a name="line106"></a> *
<a name="line107"></a> * @param {Array.&lt;number&gt;|Uint8Array} input An array of bytes (numbers with
<a name="line108"></a> *     value in [0, 255]) to encode.
<a name="line109"></a> * @param {boolean=} opt_webSafe Boolean indicating we should use the
<a name="line110"></a> *     alternative alphabet.
<a name="line111"></a> * @return {string} The base64 encoded string.
<a name="line112"></a> */
<a name="line113"></a>goog.crypt.base64.encodeByteArray = function(input, opt_webSafe) {
<a name="line114"></a>  if (!goog.isArrayLike(input)) {
<a name="line115"></a>    throw Error(&#39;encodeByteArray takes an array as a parameter&#39;);
<a name="line116"></a>  }
<a name="line117"></a>
<a name="line118"></a>  goog.crypt.base64.init_();
<a name="line119"></a>
<a name="line120"></a>  var byteToCharMap = opt_webSafe ?
<a name="line121"></a>                      goog.crypt.base64.byteToCharMapWebSafe_ :
<a name="line122"></a>                      goog.crypt.base64.byteToCharMap_;
<a name="line123"></a>
<a name="line124"></a>  var output = [];
<a name="line125"></a>
<a name="line126"></a>  for (var i = 0; i &lt; input.length; i += 3) {
<a name="line127"></a>    var byte1 = input[i];
<a name="line128"></a>    var haveByte2 = i + 1 &lt; input.length;
<a name="line129"></a>    var byte2 = haveByte2 ? input[i + 1] : 0;
<a name="line130"></a>    var haveByte3 = i + 2 &lt; input.length;
<a name="line131"></a>    var byte3 = haveByte3 ? input[i + 2] : 0;
<a name="line132"></a>
<a name="line133"></a>    var outByte1 = byte1 &gt;&gt; 2;
<a name="line134"></a>    var outByte2 = ((byte1 &amp; 0x03) &lt;&lt; 4) | (byte2 &gt;&gt; 4);
<a name="line135"></a>    var outByte3 = ((byte2 &amp; 0x0F) &lt;&lt; 2) | (byte3 &gt;&gt; 6);
<a name="line136"></a>    var outByte4 = byte3 &amp; 0x3F;
<a name="line137"></a>
<a name="line138"></a>    if (!haveByte3) {
<a name="line139"></a>      outByte4 = 64;
<a name="line140"></a>
<a name="line141"></a>      if (!haveByte2) {
<a name="line142"></a>        outByte3 = 64;
<a name="line143"></a>      }
<a name="line144"></a>    }
<a name="line145"></a>
<a name="line146"></a>    output.push(byteToCharMap[outByte1],
<a name="line147"></a>                byteToCharMap[outByte2],
<a name="line148"></a>                byteToCharMap[outByte3],
<a name="line149"></a>                byteToCharMap[outByte4]);
<a name="line150"></a>  }
<a name="line151"></a>
<a name="line152"></a>  return output.join(&#39;&#39;);
<a name="line153"></a>};
<a name="line154"></a>
<a name="line155"></a>
<a name="line156"></a>/**
<a name="line157"></a> * Base64-encode a string.
<a name="line158"></a> *
<a name="line159"></a> * @param {string} input A string to encode.
<a name="line160"></a> * @param {boolean=} opt_webSafe If true, we should use the
<a name="line161"></a> *     alternative alphabet.
<a name="line162"></a> * @return {string} The base64 encoded string.
<a name="line163"></a> */
<a name="line164"></a>goog.crypt.base64.encodeString = function(input, opt_webSafe) {
<a name="line165"></a>  // Shortcut for Mozilla browsers that implement
<a name="line166"></a>  // a native base64 encoder in the form of &quot;btoa/atob&quot;
<a name="line167"></a>  if (goog.crypt.base64.HAS_NATIVE_SUPPORT &amp;&amp; !opt_webSafe) {
<a name="line168"></a>    return goog.global.btoa(input);
<a name="line169"></a>  }
<a name="line170"></a>  return goog.crypt.base64.encodeByteArray(
<a name="line171"></a>      goog.crypt.stringToByteArray(input), opt_webSafe);
<a name="line172"></a>};
<a name="line173"></a>
<a name="line174"></a>
<a name="line175"></a>/**
<a name="line176"></a> * Base64-decode a string.
<a name="line177"></a> *
<a name="line178"></a> * @param {string} input to decode.
<a name="line179"></a> * @param {boolean=} opt_webSafe True if we should use the
<a name="line180"></a> *     alternative alphabet.
<a name="line181"></a> * @return {string} string representing the decoded value.
<a name="line182"></a> */
<a name="line183"></a>goog.crypt.base64.decodeString = function(input, opt_webSafe) {
<a name="line184"></a>  // Shortcut for Mozilla browsers that implement
<a name="line185"></a>  // a native base64 encoder in the form of &quot;btoa/atob&quot;
<a name="line186"></a>  if (goog.crypt.base64.HAS_NATIVE_SUPPORT &amp;&amp; !opt_webSafe) {
<a name="line187"></a>    return goog.global.atob(input);
<a name="line188"></a>  }
<a name="line189"></a>  return goog.crypt.byteArrayToString(
<a name="line190"></a>      goog.crypt.base64.decodeStringToByteArray(input, opt_webSafe));
<a name="line191"></a>};
<a name="line192"></a>
<a name="line193"></a>
<a name="line194"></a>/**
<a name="line195"></a> * Base64-decode a string.
<a name="line196"></a> *
<a name="line197"></a> * @param {string} input to decode (length not required to be a multiple of 4).
<a name="line198"></a> * @param {boolean=} opt_webSafe True if we should use the
<a name="line199"></a> *     alternative alphabet.
<a name="line200"></a> * @return {Array} bytes representing the decoded value.
<a name="line201"></a> */
<a name="line202"></a>goog.crypt.base64.decodeStringToByteArray = function(input, opt_webSafe) {
<a name="line203"></a>  goog.crypt.base64.init_();
<a name="line204"></a>
<a name="line205"></a>  var charToByteMap = opt_webSafe ?
<a name="line206"></a>                      goog.crypt.base64.charToByteMapWebSafe_ :
<a name="line207"></a>                      goog.crypt.base64.charToByteMap_;
<a name="line208"></a>
<a name="line209"></a>  var output = [];
<a name="line210"></a>
<a name="line211"></a>  for (var i = 0; i &lt; input.length; ) {
<a name="line212"></a>    var byte1 = charToByteMap[input.charAt(i++)];
<a name="line213"></a>
<a name="line214"></a>    var haveByte2 = i &lt; input.length;
<a name="line215"></a>    var byte2 = haveByte2 ? charToByteMap[input.charAt(i)] : 0;
<a name="line216"></a>    ++i;
<a name="line217"></a>
<a name="line218"></a>    var haveByte3 = i &lt; input.length;
<a name="line219"></a>    var byte3 = haveByte3 ? charToByteMap[input.charAt(i)] : 0;
<a name="line220"></a>    ++i;
<a name="line221"></a>
<a name="line222"></a>    var haveByte4 = i &lt; input.length;
<a name="line223"></a>    var byte4 = haveByte4 ? charToByteMap[input.charAt(i)] : 0;
<a name="line224"></a>    ++i;
<a name="line225"></a>
<a name="line226"></a>    if (byte1 == null || byte2 == null ||
<a name="line227"></a>        byte3 == null || byte4 == null) {
<a name="line228"></a>      throw Error();
<a name="line229"></a>    }
<a name="line230"></a>
<a name="line231"></a>    var outByte1 = (byte1 &lt;&lt; 2) | (byte2 &gt;&gt; 4);
<a name="line232"></a>    output.push(outByte1);
<a name="line233"></a>
<a name="line234"></a>    if (byte3 != 64) {
<a name="line235"></a>      var outByte2 = ((byte2 &lt;&lt; 4) &amp; 0xF0) | (byte3 &gt;&gt; 2);
<a name="line236"></a>      output.push(outByte2);
<a name="line237"></a>
<a name="line238"></a>      if (byte4 != 64) {
<a name="line239"></a>        var outByte3 = ((byte3 &lt;&lt; 6) &amp; 0xC0) | byte4;
<a name="line240"></a>        output.push(outByte3);
<a name="line241"></a>      }
<a name="line242"></a>    }
<a name="line243"></a>  }
<a name="line244"></a>
<a name="line245"></a>  return output;
<a name="line246"></a>};
<a name="line247"></a>
<a name="line248"></a>
<a name="line249"></a>/**
<a name="line250"></a> * Lazy static initialization function. Called before
<a name="line251"></a> * accessing any of the static map variables.
<a name="line252"></a> * @private
<a name="line253"></a> */
<a name="line254"></a>goog.crypt.base64.init_ = function() {
<a name="line255"></a>  if (!goog.crypt.base64.byteToCharMap_) {
<a name="line256"></a>    goog.crypt.base64.byteToCharMap_ = {};
<a name="line257"></a>    goog.crypt.base64.charToByteMap_ = {};
<a name="line258"></a>    goog.crypt.base64.byteToCharMapWebSafe_ = {};
<a name="line259"></a>    goog.crypt.base64.charToByteMapWebSafe_ = {};
<a name="line260"></a>
<a name="line261"></a>    // We want quick mappings back and forth, so we precompute two maps.
<a name="line262"></a>    for (var i = 0; i &lt; goog.crypt.base64.ENCODED_VALS.length; i++) {
<a name="line263"></a>      goog.crypt.base64.byteToCharMap_[i] =
<a name="line264"></a>          goog.crypt.base64.ENCODED_VALS.charAt(i);
<a name="line265"></a>      goog.crypt.base64.charToByteMap_[goog.crypt.base64.byteToCharMap_[i]] = i;
<a name="line266"></a>      goog.crypt.base64.byteToCharMapWebSafe_[i] =
<a name="line267"></a>          goog.crypt.base64.ENCODED_VALS_WEBSAFE.charAt(i);
<a name="line268"></a>      goog.crypt.base64.charToByteMapWebSafe_[
<a name="line269"></a>          goog.crypt.base64.byteToCharMapWebSafe_[i]] = i;
<a name="line270"></a>    }
<a name="line271"></a>  }
<a name="line272"></a>};
</pre>


</body>
</html>
